package com.nowar.data.frame;

import com.nowar.data.constant.DataConstant;
import com.nowar.data.entity.context.DataSourceContext;
import com.nowar.data.frame.context.DataSourceThreadLocal;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.util.StringUtils;

/**
 * AbstractRoutingDataSource
 *
 * @Author: 谢无殇。
 * @Date : 2023-01-15 23:29
 */
public class DataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        DataSourceContext context = DataSourceThreadLocal.get();
        String key = context.getDataSourceKey();
        DataSourceThreadLocal.remove();
        return key;
    }

    public static void setRoutingKey(String routingKey) {
        DataSourceThreadLocal.setDataSourceContext(DataSourceContext.builder().
                dataSourceKey(routingKey).build());
    }
}
